/**
 * StudyTimeServicee.java
 * Copyright (c) 2018, 北京科技有限公司版权所有.
*/

package com.sftz.modules.datacollect.analytics.service.Impl;

import java.io.IOException;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.List;

import org.nutz.json.Json;

import com.google.common.io.Resources;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.sftz.framework.vendors.elasticsearch.EsSearchUtil;
import com.sftz.modules.datacollect.analytics.dto.BehaviorStudyTimeDto;
import com.sftz.modules.datacollect.analytics.dto.UserBehaviorLogDto;
import com.sftz.modules.datacollect.analytics.service.StudyRecordTimeService;
import com.uxuexi.core.common.util.Util;

/**
 * @author   DuKL  为学习页提供的接口
 * @Date	 2019年1月21日
 */
public class StudyRecordTimeServiceImpl implements StudyRecordTimeService {

	/**
	 * 课程最后一次学习的资源及时间
	 */
	@Override
	public BehaviorStudyTimeDto lastStudyTime(int courseId, int userId) {
		String indexName = "logstash-study*";
		String tmplPath = "elasticsearch/templates/dukl/study/log_query_study_course.js";
		URL url = Resources.getResource(tmplPath);
		String query;
		try {
			query = Resources.toString(url, Charset.forName("utf-8"));
			query = query.replaceAll("@courseId", courseId + "");
			query = query.replaceAll("@userId", userId + "");
			List<UserBehaviorLogDto> lst = EsSearchUtil.searchTmpl(query, indexName, UserBehaviorLogDto.class);
			if (!Util.isEmpty(lst)) {
				String json = Json.toJson(lst);
				Gson gs = new Gson();
				List<UserBehaviorLogDto> jsonListObject = gs.fromJson(json, new TypeToken<List<UserBehaviorLogDto>>() {
				}.getType());
				BehaviorStudyTimeDto studyDto = null;
				for (UserBehaviorLogDto userBehaviorLogDto : jsonListObject) {
					studyDto = gs.fromJson(Json.toJson(userBehaviorLogDto.getRequest_params()),
							BehaviorStudyTimeDto.class);
				}
				return studyDto;
			} else {
				return null;
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 某视频最后一次学习到的时间
	 */
	@Override
	public BehaviorStudyTimeDto lastStudyVideoTime(int videoId, int userId) {
		String indexName = "logstash-study*";
		String tmplPath = "elasticsearch/templates/dukl/study/log_query_study_course_video.js";
		URL url = Resources.getResource(tmplPath);
		String query;
		try {
			query = Resources.toString(url, Charset.forName("utf-8"));
			query = query.replaceAll("@vid", videoId + "");
			query = query.replaceAll("@userId", userId + "");
			List<UserBehaviorLogDto> lst = EsSearchUtil.searchTmpl(query, indexName, UserBehaviorLogDto.class);
			if (!Util.isEmpty(lst)) {
				String json = Json.toJson(lst);
				Gson gs = new Gson();
				List<UserBehaviorLogDto> jsonListObject = gs.fromJson(json, new TypeToken<List<UserBehaviorLogDto>>() {
				}.getType());
				BehaviorStudyTimeDto studyDto = null;
				for (UserBehaviorLogDto userBehaviorLogDto : jsonListObject) {
					studyDto = gs.fromJson(Json.toJson(userBehaviorLogDto.getRequest_params()),
							BehaviorStudyTimeDto.class);
				}
				return studyDto;
			} else {
				return null;
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 某音频最后一次学习到的时间
	 */
	@Override
	public BehaviorStudyTimeDto lastStudyAudioTime(int audioId, int userId) {
		String indexName = "logstash-study*";
		String tmplPath = "elasticsearch/templates/dukl/study/log_query_study_course_audio.js";
		URL url = Resources.getResource(tmplPath);
		String query;
		try {
			query = Resources.toString(url, Charset.forName("utf-8"));
			query = query.replaceAll("@audioId", audioId + "");
			query = query.replaceAll("@userId", userId + "");
			List<UserBehaviorLogDto> lst = EsSearchUtil.searchTmpl(query, indexName, UserBehaviorLogDto.class);
			if (!Util.isEmpty(lst)) {
				String json = Json.toJson(lst);
				Gson gs = new Gson();
				List<UserBehaviorLogDto> jsonListObject = gs.fromJson(json, new TypeToken<List<UserBehaviorLogDto>>() {
				}.getType());
				BehaviorStudyTimeDto studyDto = null;
				for (UserBehaviorLogDto userBehaviorLogDto : jsonListObject) {
					studyDto = gs.fromJson(Json.toJson(userBehaviorLogDto.getRequest_params()),
							BehaviorStudyTimeDto.class);
				}
				return studyDto;
			} else {
				return null;
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

}
